package com.adinall.dsp.core.poi.excel;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.adinall.dsp.core.utils.StringUtils;

public class Read extends Excel {

    private XSSFWorkbook workbook;

    public Read(String filePath) {

        InputStream is = null;
        try {
            is = new FileInputStream(filePath);
            workbook = new XSSFWorkbook(is);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public Read(InputStream is) {
        try {
            workbook = new XSSFWorkbook(is);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public List<Map<Integer, Object>> asMap() {

        XSSFSheet sheet = workbook.getSheetAt(0);

        List<Map<Integer, Object>> data = new ArrayList<Map<Integer, Object>>();

        for (int i = 0; i <= sheet.getLastRowNum(); i++) {

            Map<Integer, Object> map = new HashMap<Integer, Object>();

            XSSFRow row = sheet.getRow(i);

            for (int j = 0; j < row.getLastCellNum(); j++) {

                XSSFCell cell = getCell(sheet, i, j);

                if (CellType.STRING == cell.getCellTypeEnum()) {

                    map.put(j, cell.getStringCellValue());

                } else if (CellType.BOOLEAN == cell.getCellTypeEnum()) {

                    map.put(j, cell.getBooleanCellValue());

                } else if (CellType.NUMERIC == cell.getCellTypeEnum()) {

                    if (DateUtil.isCellDateFormatted(cell)) {
                        map.put(j, DateUtil.getJavaCalendar(cell.getNumericCellValue()));
                    } else {

                        // 百分比
                        if (cell.getCellStyle().getDataFormatString().endsWith("%")) {
                            map.put(j, StringUtils.toString(cell.getNumericCellValue() * 100));
                        } else {
                            map.put(j, new DecimalFormat("0.00").format(cell.getNumericCellValue()));
                        }
                    }

                } else if (CellType.BLANK == cell.getCellTypeEnum()) {

                    map.put(j, "");
                } else if (CellType._NONE == cell.getCellTypeEnum()) {

                    map.put(j, null);
                } else if (CellType.FORMULA == cell.getCellTypeEnum()) {

                    map.put(j, cell.getNumericCellValue());
                } else {

                    throw new RuntimeException("不能识别的类型");
                }
            }

            data.add(map);
        }

        if (workbook != null) {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return data;
    }
}
